package examples.collections.model;

import java.util.Comparator;
import java.util.Date;

public class Book implements Comparable<Book> {

	public enum Category {
		art, biography, business, children, computing, hobbies
	}

	private final long id;
	private final String title;
	private String author;
	private Category category;
	private Date publicationDate;
	private String edition;

	public Book(long id, String title) {
		assert (title != null);
		this.id = id;
		this.title = title;
	}

	public String getTitle() {
		return title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public Date getPublicationDate() {
		return publicationDate;
	}

	public void setPublicationDate(Date publicationDate) {
		this.publicationDate = publicationDate;
	}

	public long getId() {
		return id;
	}

	public String getEdition() {
		return edition;
	}

	public void setEdition(String edition) {
		this.edition = edition;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((author == null) ? 0 : author.hashCode());
		result = prime * result + ((edition == null) ? 0 : edition.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Book other = (Book) obj;
		if (author == null) {
			if (other.author != null)
				return false;
		} else if (!author.equals(other.author))
			return false;
		if (edition == null) {
			if (other.edition != null)
				return false;
		} else if (!edition.equals(other.edition))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Book [id=" + id + ", title=" + title + ", author=" + author
				+ ", category=" + category + ", edition=" + edition
				+ ", publicationDate=" + publicationDate + "]";
	}

	public int compareTo(Book other) {
		int cmp = title.compareTo(other.title);
		if (cmp != 0) {
			return cmp;
		}
		if (author == null && other.author == null) {
			return 0;
		} else if (other.author == null) {
			return -1;
		} else if (author == null) {
			return 1;
		}
		
		return author.compareTo(other.author);
	}

	public static final Comparator<Book> PUBLICATION_ORDER = new Comparator<Book>() {
		public int compare(Book b1, Book b2) {
			if (b1.publicationDate == null && b2.publicationDate == null) {
				return 0;
			} else if (b2.publicationDate == null) {
				return -1;
			} else if (b1.publicationDate == null) {
				return 1;
			}
			return b1.publicationDate.compareTo(b2.publicationDate);
		}
	};
}
